﻿var _loaded = false;
var _ready = true;
var _view = null;
var _quadrantTemplate = null;

$(function() {
    _loaded = true;
    if (_ready) { RunTests(); }
});

function SciReady(e) {
    _ready = true;
    if (_loaded) { RunTests(); }
}

sci.SetBasePath('../JavaScript/');
sci.AddReadyListener(this);
sci.Require('sci.quadrant.QuadrantController');

function RunTests() {
    var bindings = $('[data-controller=Tests]');
    _view = bindings.filter('[data-name=ValidQuadrantContainer]');
    _quadrantTemplate = $(bindings.filter('[data-name=QuadrantTemplate]').html());

    var tests = new QuadrantTests();
    //start(); 

    module('Initialization', { setup: tests.SetUp });
    test('QuadrantController cannot be created if a view is not provided', tests.QuadrantControllerCannotBeCreatedIfViewIsNotProvided);
    test('QuadrantController cannot be created if the view does not contain a canvas', tests.QuadrantControllerCannotBeCreatedIfTheViewDoesNotContainCanvas);
    test('QuadrantController cannot be created if the component does not specify an initial image name', tests.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyAnInitialImageName);
    test('QuadrantController cannot be created if the component does not specify an image width', tests.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyImageWidth);
    test('QuadrantController cannot be created if the component does not specify an image height', tests.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyImageHeight);
    test('Can initialize', tests.CanInitialize);

    module('Interaction', { setup: tests.SetUp });
    test('Can select a different zoom level', tests.CanSelectDifferentZoomLevel);
    //test('Invalid when other type is seleceted and name is empty', tests.InvalidWhenOtherIdOtherTypeIsSelectedAndNameIsEmpty);
}

function QuadrantTests() {
    this.SetUp = function () {
        _view.html('');
    };

    this.QuadrantControllerCannotBeCreatedIfViewIsNotProvided = function () {
        // Arrange
        var message = '';

        // Act
        try {
            new sci.quadrant.QuadrantController();
        }
        catch (ex) {
            message = ex;
        }

        // Assert
        equal('sci.quadrant.QuadrantController :: A view is required', message, 'View required exception is thrown.');
    };

    this.QuadrantControllerCannotBeCreatedIfTheViewDoesNotContainCanvas = function () {
        // Arrange
        var viewWithNoCanvas = document.getElementById('QuadrantContainerWithoutCanvas');
        var message = '';

        // Act
        try {
            new sci.quadrant.QuadrantController(viewWithNoCanvas);
        }
        catch (ex) {
            message = ex;
        }

        // Assert
        equal('sci.quadrant.QuadrantController :: Quadrant was not able to find an element in the specified view with attributes data-controller="Quadrant" and data-name="Canvas"',
            message, 'Canvas required exception is thrown.');
    };

    this.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyAnInitialImageName = function () {
        // Arrange
        var viewWithNoName = document.getElementById('QuadrantContainerWithoutImageName');
        var message = '';

        // Act
        try {
            new sci.quadrant.QuadrantController(viewWithNoName);
        }
        catch (ex) {
            message = ex;
        }

        // Assert
        equal('sci.quadrant.QuadrantController :: The attribute data-image-name is required',
            message, 'Initial image name required exception is thrown.');
    };

    this.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyImageWidth = function () {
        // Arrange
        var viewWithNoWidth = document.getElementById('QuadrantContainerWithoutWidth');
        var message = '';

        // Act
        try {
            new sci.quadrant.QuadrantController(viewWithNoWidth);
        }
        catch (ex) {
            message = ex;
        }

        // Assert
        equal('sci.quadrant.QuadrantController :: The attribute data-image-width for the Quadrant Component is either missing or less than 10',
            message, 'Width required exception is thrown.');
    };

    this.QuadrantControllerCannotBeCreatedIfTheComponentDoesNotSpecifyImageHeight = function () {
        // Arrange
        var viewWithNoHeight = document.getElementById('QuadrantContainerWithoutHeight');
        var message = '';

        // Act
        try {
            new sci.quadrant.QuadrantController(viewWithNoHeight);
        }
        catch (ex) {
            message = ex;
        }

        // Assert
        equal('sci.quadrant.QuadrantController :: The attribute data-image-height for the Quadrant Component is either missing or less than 10',
            message, 'Height required exception is thrown.');
    };

    this.CanInitialize = function () {
        // Arrange - Act
        var controller = new sci.quadrant.QuadrantController(_quadrantTemplate.clone().appendTo(_view)[0]);

        // Assert
        ok(true, 'Initialized');
    };

    this.CanSelectDifferentZoomLevel = function () {
        // Arrange
        var view = _quadrantTemplate.clone().appendTo(_view);
        var controller = new sci.quadrant.QuadrantController(view[0]);

        // Act
        view.find('.controls .large').trigger('click');

        // Assert
        equal(120, 120, 'The zoom level is now 120%');
    };
}